package factorytest.iflytek.com.einkiflytekfactorytest.utils;

import android.content.Context;

import java.util.Formatter;
import java.util.Locale;

import factorytest.iflytek.com.einkiflytekfactorytest.R;

/**
 * Created by sst on 2018/10/30.
 */

public class TimeStringUtils {

    /*  Try to use String.format() as little as possible, because it creates a
    *  new Formatter every time you call it, which is very inefficient.
    *  Reusing an existing Formatter more than tripled the speed of
    *  makeTimeString().
    *  This Formatter/StringBuilder are also used by makeAlbumSongsLabel()
    */
    private static StringBuilder sFormatBuilder = new StringBuilder();
    private static Formatter sFormatter = new Formatter(sFormatBuilder, Locale.getDefault());
    private static final Object[] sTimeArgs = new Object[5];

    private static final String durationformatshort = "%02$d:%5$02d";
    private static final String durationformatlong = "%1$02d:%3$02d:%5$02d";

    /*

    <!-- Do not translate. Duration format for duration < 1 hour -->
    <string name="durationformatshort" translatable="false"><xliff:g id="format">%02$d:%5$02d</xliff:g></string>
    <!-- Do not translate. Duration format for duration >= 1 hour -->
    <string name="durationformatlong" translatable="false"><xliff:g id="format">%1$02d:%3$02d:%5$02d</xliff:g></string>

     */
    public static String makeTimeString(long ms) {
        long secs = ms / 1000;
        String durationformat = secs < 3600 ? durationformatshort : durationformatlong;

        /* Provide multiple arguments so the format can be changed easily
         * by modifying the xml.
         */
        sFormatBuilder.setLength(0);

        final Object[] timeArgs = sTimeArgs;
        timeArgs[0] = secs / 3600;
        timeArgs[1] = secs / 60;
        timeArgs[2] = (secs / 60) % 60;
        timeArgs[3] = secs;
        timeArgs[4] = secs % 60;

        return sFormatter.format(durationformat, timeArgs).toString();
    }
}
